/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.easydarwin.util;

import android.content.Context;
import android.content.SharedPreferences;

import org.easydarwin.conf.Config;

public class PreferenceManager {
	/**
	 * 保存Preference的name
	 */
	public static final String PREFERENCE_NAME = "easyRTC";
	private static SharedPreferences mSharedPreferences;
	private static PreferenceManager mPreferencemManager;
	private static SharedPreferences.Editor editor;

	private static String SHARED_KEY_CURRENTUSER_USERNAME = "SHARED_KEY_CURRENTUSER_USERNAME";
	private static String SHARED_KEY_CURRENTUSER_PASSWORD = "SHARED_KEY_CURRENTUSER_PASSWORD";
	private static String USER_ROLE = "SHARED_KEY_USER_ROLE";
	private static String VIDEO_URL_IP = "VIDEO_URL_IP";
	private static String VIDEO_URL_PORT = "VIDEO_URL_PORT";
	private static String VIDEO_URL_ID = "VIDEO_URL_ID";
	private static String VIDEO_PEER_ID = "VIDEO_PEER_ID";
	private static String USER_ACCOUNT = "USER_ACCOUNT";
	private static String touser_account = "TOUSER_ACCOUNT";

	private static String PLAY_URL = "PLAY_URL";
	
	private PreferenceManager(Context cxt) {
		mSharedPreferences = cxt.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		editor = mSharedPreferences.edit();
	}

	public static synchronized void init(Context cxt) {
		if (mPreferencemManager == null) {
			mPreferencemManager = new PreferenceManager(cxt);
		}
	}

	/**
	 * 单例模式，获取instance实例
	 * 
	 * @return
	 */
	public synchronized static PreferenceManager getInstance() {
		if (mPreferencemManager == null) {
			throw new RuntimeException("please init first!");
		}
		return mPreferencemManager;
	}


	public void setAccount(String account) {
		editor.putString(USER_ACCOUNT, account);
		editor.commit();
	}

	public String getAccount() {
		return mSharedPreferences.getString(USER_ACCOUNT, null);
	}

	public void setCurrentUserName(String username) {
		editor.putString(SHARED_KEY_CURRENTUSER_USERNAME, username);
		editor.commit();
	}

	public String getCurrentUsername() {
		return mSharedPreferences.getString(SHARED_KEY_CURRENTUSER_USERNAME, null);
	}

	public void setCurrentUserPwd(String password){
		editor.putString(SHARED_KEY_CURRENTUSER_PASSWORD, password);
		editor.apply();
	}
	
	public String getCurrentUserPwd(){
		return mSharedPreferences.getString(SHARED_KEY_CURRENTUSER_PASSWORD, null);
	}

	public void setRole(String role){
		editor.putString(USER_ROLE , role);
		editor.apply();
	}

	public String getRole(String roleCode){
		return mSharedPreferences.getString(USER_ROLE , roleCode);
	}

	public void setIP(String ip){
		editor.putString(VIDEO_URL_IP , ip);
		editor.apply();
	}

	public String getIP(){
		return mSharedPreferences.getString(VIDEO_URL_IP , Config.DEFAULT_SERVER_IP);
	}

	public void setPort(String port){
		editor.putString(VIDEO_URL_PORT , port);
		editor.apply();
	}

	public String getPort(){
		return mSharedPreferences.getString(VIDEO_URL_PORT , Config.DEFAULT_SERVER_PORT_CHECK);
	}

	public void setPushID(String port){
		editor.putString(VIDEO_URL_ID , port);
		editor.apply();
	}

	public String getPushID(String defaultId){
		return mSharedPreferences.getString(VIDEO_URL_ID , defaultId);
	}


	public void setPlayId(String peerId){
		editor.putString(VIDEO_PEER_ID , peerId);
		editor.apply();
	}

	public String getPlayId(String defaultPeerId){
		return mSharedPreferences.getString(VIDEO_PEER_ID , defaultPeerId);
	}

	public void setFriendAccount(String account){
		editor.putString(touser_account , account);
		editor.apply();
	}

	public String getFriendAccount(String defaultAccount){
		return mSharedPreferences.getString(touser_account , defaultAccount);
	}

	public void setPlayUrl(String url){
		editor.putString(PLAY_URL , url);
		editor.apply();
	}

	public String getPlayUrl(String url){
		return mSharedPreferences.getString(PLAY_URL , url);
	}
}
